﻿using Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.Hanlder
{
    public class User_addressHandler : IHttpHandler
    {
        #region Base
        HttpRequest Request;
        HttpResponse Response;
        System.Web.SessionState.HttpSessionState Session;
        HttpServerUtility Server;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            Request = context.Request;
            Response = context.Response;
            Session = context.Session;
            Server = context.Server;
            string method = Request["Method"].ToString();
            System.Reflection.MethodInfo methodInfo = this.GetType().GetMethod(method);
            methodInfo.Invoke(this, null);
        }
        public List<Model.tb_user_address> GetOBJList()
        {
            return Common.SqlExption.GetList<Model.tb_user_address>();
        }
        public void GetList()
        {
            var data = Common.SqlExption.GetList<Model.tb_user_address>().AsQueryable();
            if (Request["Id"] != null)
            {
 if (Request["Id"].ToString().Trim().Length>0)
                {
                int txt_search = Convert.ToInt32( Request["Id"]);
 if (txt_search>0)
                {
                data = data.Where(c => c.Id.Equals(txt_search));
            }
                }
            }
            if (Request["user_number"] != null)
            {
 if (Request["user_number"].ToString().Trim().Length>0)
                {
                string txt_search = Request["user_number"];
 if (txt_search.Trim().Length>0)
                {
                data = data.Where(c => c.user_number.Contains(txt_search));
            }
                }
            }
            if (Request["address_id"] != null)
            {
 if (Request["address_id"].ToString().Trim().Length>0)
                {
                int txt_search = Convert.ToInt32( Request["address_id"]);
 if (txt_search>0)
                {
                data = data.Where(c => c.address_id.Equals(txt_search));
            }
                }
            }
            string result = JsonConvert.SerializeObject(data.ToList());
            Response.Write(result);
            Response.End();

        }
        public void Del()
        {
            ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            int Id = int.Parse(Request["Id"]);

            var model = GetOBJList().FirstOrDefault(c => c.Id.Equals(Id));
            if (model != null)
            {
                Common.SqlExption.Delete<Model.tb_user_address>(Id);
            }
            rh.states = 0;
            string jsonSucess = JsonConvert.SerializeObject(rh);

            Response.Write(jsonSucess);
            Response.End();
        }
        public void Edit()
        {


            ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
 int Id=0;
            if (Request["Id"] != null)
            {
 if ( !int.TryParse(Request["Id"],out Id) )
{
 if (Id<0)
{
rh.states = 1;
rh.msg = " 验证错误！";
string json = JsonConvert.SerializeObject(rh);
 Response.Write(json);
Response.End();
}
}
}
 string user_number=string.Empty;
            if (Request["user_number"] != null)
            {
  user_number = Request["user_number"];
if (user_number.Trim().Length <= 0)
 {
rh.states = 1;
rh.msg = " 验证错误！";
string json = JsonConvert.SerializeObject(rh);
 Response.Write(json);
Response.End();
}
}
 int address_id=0;
            if (Request["address_id"] != null)
            {
 if ( !int.TryParse(Request["address_id"],out address_id) )
{
 if (address_id<0)
{
rh.states = 1;
rh.msg = " 验证错误！";
string json = JsonConvert.SerializeObject(rh);
 Response.Write(json);
Response.End();
}
}
}
            var model = Common.SqlExption.GetList<Model.tb_user_address>().FirstOrDefault(c => c.Id.Equals(Id));
            if (model == null)
            {
                model = new Model.tb_user_address();
model.Id=Id;
model.user_number=user_number;
model.address_id=address_id;
                Common.SqlExption.Add(model);
            }
            else
            {
model.Id=Id;
model.user_number=user_number;
model.address_id=address_id;
                Common.SqlExption.Update(model);
            }
            rh.states = 0;
            string jsonSucess = JsonConvert.SerializeObject(rh);
           
            Response.Write(jsonSucess);
            Response.End();
         
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        #endregion
    }
}
